package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.commonactivity.data.CommonActivityAutoBuyData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonActivityAutoBuyDataDaoImpl extends TempDataObjectDao<CommonActivityAutoBuyData> {

    public static String getCommonActivityAutoBuyDataSql = "select * from `t_u_common_activity_auto_buy_data` where `userId`=?";
    public Map<Integer, CommonActivityAutoBuyData> getCommonActivityAutoBuyData(long userId) {

        Map<Integer, CommonActivityAutoBuyData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getCommonActivityAutoBuyDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonActivityAutoBuyData parse = parse(rs);
                map.put(parse.getChildType(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCommonActivityAutoBuyDataSql = "update `t_u_common_activity_auto_buy_data` set chooseMap=? where userId=? and childType=?";
    @Override
    protected boolean updateData(CommonActivityAutoBuyData commonActivityAutoBuyData) {
        commonActivityAutoBuyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(commonActivityAutoBuyData.getChooseMap())));
        para.put(i++, new DbParameter(Types.BIGINT, commonActivityAutoBuyData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonActivityAutoBuyData.getChildType()));
        if (getDb().executeNoneQuery(updateCommonActivityAutoBuyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCommonActivityAutoBuyDataSql = "insert into `t_u_common_activity_auto_buy_data`(userId,childType,chooseMap) values (?,?,?)";
    @Override
    protected boolean addData(CommonActivityAutoBuyData commonActivityAutoBuyData) {
        commonActivityAutoBuyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, commonActivityAutoBuyData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonActivityAutoBuyData.getChildType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(commonActivityAutoBuyData.getChooseMap())));
        if (getDb().executeNoneQuery(insertCommonActivityAutoBuyDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CommonActivityAutoBuyData parseData(ResultSet rs) throws SQLException {
        CommonActivityAutoBuyData commonActivityAutoBuyData = new CommonActivityAutoBuyData();
        commonActivityAutoBuyData.setUserId(rs.getLong("userId"));
        commonActivityAutoBuyData.setChildType(rs.getInt("childType"));
        commonActivityAutoBuyData.setChooseMap(JSON.parseObject(rs.getString("chooseMap"), new TypeReference<Map<String, Integer>>() {}));

        return commonActivityAutoBuyData;
    }


}
